#ifndef MAIN_INTERFACE_H
#define MAIN_INTERFACE_H

#include <QMainWindow>

#include "tcp_server.h"
#include "deal_network_status.h"
#include "wait_processbar.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainInterface;
}
QT_END_NAMESPACE

class MainInterface : public QMainWindow
{
    Q_OBJECT

public:
    MainInterface(QWidget *parent = nullptr);
    ~MainInterface();

private:
    Ui::MainInterface *ui;


//界面
private:
    /**
     * @brief initializeMainInterfaceSetStyleSheet：设置主界面的界面风格
     */
    void initializeMainInterfaceSetStyleSheet();

    void initializeExamineExamManagePageSetStyleSheet();

    void initializeRouteExamManagePageSetStyleSheet();

    void initializeTampingHomeworkExamManagePageSetStyleSheet();

    //界面切换
signals:
    /**
     * @brief 展示登录界面
     */
    void show2LoginDialog(int interfaceType, int examType, int examPatternType);


protected:
    /**
     * @brief 事件过滤器
     * @param obj
     * @param event
     * @return
     */
    bool eventFilter(QObject *obj, QEvent *event);


//网络
private:
    //tcp服务器
    TcpServer *m_server;
    //监听的ip和端口号
    QString m_listenIp;
    int m_listenPort = 11000;

    /**
     * @brief 获得全部ip
     * @return
     */
    QStringList getLocalIPs();
    /**
     * @brief 获得可用的192ip
     * @return
     */
    QString getLocalIP();
    /**
     * @brief 开启tcp服务
     */
    void startTcpServer();
    /**
     * @brief 初始化TCP服务器
     */
    void initializeTcpServer();

signals:
    //关闭服务器
    void closeServer();
    //关闭套接字
    void closeAllSocket();
    //关闭全部套接字
    void closeOneSocket(const QString &ip, int port);

private slots:
    /**
     * @brief 单个客户端建立连接
     * @param ip
     * @param port
     */
    void clientConnected(const QString &ip, int port);
    /**
     * @brief 单个客户端断开连接
     * @param ip
     * @param port
     */
    void clientDisconnected(const QString &ip, int port);

public slots:
    /**
     * @brief 发送登录的信息
     * @param uername
     */
    void sendLoginInformation(QString username, int equipmentType, int examType, int examPatternType, QHash<QString, QList<int>> faultType);


//监控服务器网络
private:
    //服务器状态
    bool m_serverStatus = false;

    //已连接的客户端（ip和端口号）
    QList<QPair<QString, int>> clientConnectedList;

    QHash<int, QPair<QString, int>> clientInformationHash;


    //监控网络状态
    //网络状况
    enum NetworkStatus{
        Normal = 0,
        Disconnect = 1,
        Maintain = 2
    };
    //网络状态的线程
    QThread m_dealNetworkStatusThread;
    //网络状态
    int m_networkStatusFlag = NetworkStatus::Disconnect;
    //服务器状态
    bool m_serverStatusFlag = true;
    //服务器计时
    int m_serverTime = 0;

    DealNetworkStatus* m_dealNetworkStatus;

    /**
     * @brief 初始化网络监听线程
     */
    void initializeNetworkStatusThread();

signals:
    //开始网络监听
    void startMonitorNetworkStatus();
    //关闭网络监听
    void stopMonitorNetworkStatus();

//数据库
private:
    QString m_databaseConnectedName = "mysql_connection";


private:
    //设备的通信状态
    bool m_examineExamEquipmentStatus = false;
    bool m_routeExamEquipmentStatus = false;
    bool m_tampingHomeworkEquipmentStatus = false;

    //设备登录的状态
    bool m_examineExamEquipmentLoginStatus = false;
    bool m_routeExamEquipmentLoginStatus = false;
    bool m_tampingHomeworkEquipmentLoginStatus = false;

    //临时存储的用户登录信息
    QString m_saveUsername;
    int m_saveEquipmentType;
    int m_saveExamType;
    int m_saveExamPatternType;
    QHash<QString, QList<int>> m_saveFaultType;

    //转圈等待界面
    WaitProcessBar* m_waitProcessBar;

////


};
#endif // MAIN_INTERFACE_H
